package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.xy.base.server.utils.PageUtil;
import com.rustic.server.query.UserLikeLinkAdminListQuery;
import com.rustic.server.dto.UserLikeLinkAddDTO;
import com.rustic.server.dto.UserLikeLinkUpdateDTO;
import com.rustic.server.po.UserLikeLink;
import com.rustic.server.service.UserLikeLinkService;
import com.rustic.server.vo.list.UserLikeLinkAdminListVO;
import com.rustic.server.vo.UserLikeLinkVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[user_like_link]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/userLikeLink")
@Api(tags = {"用户点赞关联管理接口"})
public class UserLikeLinkAdminController {

    @Autowired
    private UserLikeLinkService userLikeLinkService;

    @ApiOperation(value = "用户点赞关联-列表", notes = "权限key: user-like-link-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('user-like-link-list')")
    public Result<Page<UserLikeLinkAdminListVO>> list(@Validated UserLikeLinkAdminListQuery query) {
        LambdaQueryWrapper<UserLikeLink> queryWrapper = userLikeLinkService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<UserLikeLink> pageData = userLikeLinkService.page(query.toPage(), queryWrapper);
        List<UserLikeLink> userLikeLinkList = pageData.getRecords();
        if (CollUtil.isEmpty(userLikeLinkList)) {
            return Result.data(new Page<>());
        }
        List<UserLikeLinkAdminListVO> results = userLikeLinkService.toAdminListVo(userLikeLinkList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "用户点赞关联-详情", notes = "权限key: user-like-link-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用户点赞关联id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('user-like-link-detail')")
    public Result<UserLikeLinkVO> detail(int id) {
        if (id <= 0) {
            return Result.error(UserLikeLinkService.USER_LIKE_LINK_NOT_EXIST);
        }
        UserLikeLink userLikeLink = userLikeLinkService.getById(id);
        if (userLikeLink == null) {
            return Result.error(UserLikeLinkService.USER_LIKE_LINK_NOT_EXIST);
        }
        UserLikeLinkVO userLikeLinkVO = new UserLikeLinkVO(userLikeLink);
        return Result.data(userLikeLinkVO);
    }

    @ApiOperation(value = "用户点赞关联-增加", notes = "权限key: user-like-link-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('user-like-link-add')")
    @OperateLog(description = "增加用户点赞关联")
    public Result<String> add(@RequestBody @Validated UserLikeLinkAddDTO userLikeLinkAddDTO) {
        UserLikeLink userLikeLink = userLikeLinkAddDTO.toUserLikeLink();
        userLikeLinkService.save(userLikeLink);
        return Result.ok("添加用户点赞关联成功");
    }

    @ApiOperation(value = "用户点赞关联-更新", notes = "权限key: user-like-link-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('user-like-link-update')")
    @OperateLog(description = "更新用户点赞关联")
    public Result<String> update(@RequestBody @Validated UserLikeLinkUpdateDTO userLikeLinkUpdateDTO) {
        UserLikeLink userLikeLink = userLikeLinkService.getById(userLikeLinkUpdateDTO.getId());
        if (userLikeLink == null) {
            return Result.error(UserLikeLinkService.USER_LIKE_LINK_NOT_EXIST);
        }
        BeanUtils.copyProperties(userLikeLinkUpdateDTO, userLikeLink);
        userLikeLinkService.updateById(userLikeLink);
        return Result.ok("编辑用户点赞关联成功");
    }

    @ApiOperation(value = "用户点赞关联-删除", notes = "权限key: user-like-link-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('user-like-link-delete')")
    @OperateLog(description = "删除用户点赞关联")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(UserLikeLinkService.USER_LIKE_LINK_NOT_EXIST);
        }
        UserLikeLink userLikeLink = userLikeLinkService.getById(id);
        if (userLikeLink == null) {
            return Result.error(UserLikeLinkService.USER_LIKE_LINK_NOT_EXIST);
        }
        userLikeLinkService.removeById(id);
        return Result.ok("删除用户点赞关联成功");
    }
}
